from collections import Iterable


# filter(function, iterable)


def is_odd(n):
    return n % 2 == 1


print(list(filter(is_odd, range(20))))


def not_empty(s):
    return s and s.strip()


print(list((filter(not_empty, ['A', '', 'B', None, 'C', '  ']))))


def _odd_iter():
    """
    奇数序列
    :return 下一个奇数
    """
    n = 1
    while True:
        n = n + 2
        yield n


def _not_divisible(n):
    return lambda x: x % n > 0  # 返回一个函数


def primes():
    yield 2
    it = _odd_iter()
    while True:
        next_num = next(it)
        yield next_num
        # 第一次相当于 _not_divisible(3)(5)  (5 % 3 == 2) > 0 ==> True
        it = filter(_not_divisible(next_num), it)  # (next_num) -> n, it -> x  _not_divisible(next_num)第一个参数n为next_num


for num in primes():
    if num < 100:
        print(num, end=' ')
    else:
        break

print()

# print(_not_divisible(2)(4))
# print(_not_divisible(next(_odd_iter())))
print(isinstance(_odd_iter(), Iterable))


def is_palindrome(n):
    """
    判断是否是回数
    12321 334433
    """
    s = str(n)
    i = 0
    length = len(s)
    while i < length:
        if s[i] == s[-i - 1]:
            if i == length:
                return True
            i = i + 1
        else:
            return False
    return False


def is_palindrome2(n):
    n = str(n)
    return n == n[::-1]   # 反转


op1 = filter(is_palindrome2, range(1, 10000))
print(list(op1))

output = filter(is_palindrome2, range(1, 10000))
print(list(output))
